package cn.legend.travel.admin.destination.controller;

import cn.legend.travel.admin.destination.pojo.param.DistrictAddNewParam;
import cn.legend.travel.admin.destination.pojo.param.DistrictUpdateParam;
import cn.legend.travel.admin.destination.service.IDistrictService;
import cn.legend.travel.common.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "区域管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("districts")
public class DistrictController {
    private final IDistrictService service;

    @PostMapping("rebuildCache")
    @ApiOperation("重建区域缓存")
    @PreAuthorize("hasAuthority('/districts/rebuild')")
    public JsonResult rebuildCache() {
        service.rebuild();
        return JsonResult.ok();
    }

    @PostMapping("delete")
    @ApiOperation("删除目的地")
    @PreAuthorize("hasAuthority('/districts/manage')")
    public JsonResult delete(Long id) {
        service.delete(id);
        return JsonResult.ok();
    }

    @GetMapping("listByParent")
    @ApiOperation("查看目的地列表")
    @PreAuthorize("hasAnyAuthority('/districts/manage','/sceneries/manage')")
    public JsonResult listByParent(@RequestParam Long parentId, Integer pageNum) {
        return JsonResult.ok(service.listByParent(parentId, pageNum == null ? 1 : pageNum));
    }

    @PostMapping("update")
    @ApiOperation("更改目的地")
    @PreAuthorize("hasAuthority('/districts/manage')")
    public JsonResult update(@RequestBody DistrictUpdateParam param) {
        service.update(param);
        return JsonResult.ok();
    }

    @PostMapping("addNew")
    @ApiOperation("新增目的地")
    @PreAuthorize("hasAuthority('/districts/addNew')")
    public JsonResult addNew(@RequestBody @Validated DistrictAddNewParam param) {
        service.addNew(param);
        return JsonResult.ok();
    }

    @GetMapping("listAllByParent")
    @ApiOperation("获取全部地区")
    @PreAuthorize("hasAnyAuthority('/districts/addNew','/districts/manage','/sceneries/manage')")
    public JsonResult listAllByParent(@RequestParam Long parentId) {
        return JsonResult.ok(service.listAllByParent(parentId));
    }
}
